﻿using CommonLib;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using YouErYuanBLL;
using YouErYuanModels;

namespace YouErYuanManage.Controllers
{

    public class RispeSettingController : Controller
    {
        /// <summary>
        /// 食谱列表
        /// </summary>
        /// <returns></returns>
        [YouErYuanManage.Filter.IsLogin]
        public string RecipeSettingPageList()
        {
            RecipeBLL ckbll = new RecipeBLL();
            int pageIndex = 0;
            int pageSize = 0;
            try
            {
                pageIndex = Convert.ToInt32(Request["pageIndex"]);
                pageSize = Convert.ToInt32(Request["pageSize"]);
            }
            catch (Exception)
            {
                pageIndex = 1;
                pageSize = 10;
            }
            //查询时间
            string times = Request["times"] + "";
            string schoolid = (HttpContext.Session["user"] as Teacher).Schoolid;
            int total = 0;
            object dt = ckbll.RispePageList(pageIndex, pageSize, schoolid, times, out total);
            int pageCount = total % pageSize == 0 ? total / pageSize : total / pageSize + 1;//总页数
            var data = new
            {
                pageIndex = pageIndex,
                pageCount = pageCount,
                list = dt,
                total = total
            };
            return CommonMethod.FormatJsonObj("", data);
        }
        [YouErYuanManage.Filter.IsLogin]
        public object SelectForId()
        {
            RecipeBLL cbll = new RecipeBLL();
            string id = Request["id"];
            object dt = cbll.SelectForId(id);
            return CommonMethod.FormatJsonObj("", dt);
        }
        /// <summary>
        /// 新增或修改
        /// </summary>
        /// <returns></returns>
        [YouErYuanManage.Filter.IsLogin]
        public string InsertOrUpdate()
        {
            RecipeBLL cbll = new RecipeBLL();
            TRecipe recipe = new TRecipe();
            string type = Request["type"];
            recipe.id = Request["id"];
            recipe.schoolId = Request["schoolId"];
            if (string.IsNullOrWhiteSpace(recipe.schoolId))
                recipe.schoolId = (HttpContext.Session["user"] as Teacher).Schoolid;
            recipe.startDate = !string.IsNullOrWhiteSpace(Request["startDate"]) ? DateTime.Parse(Request["startDate"]) : new Nullable<DateTime>();
            recipe.type = Request["sType"];
            recipe.footName = Request["footName"];
            recipe.footImg = Request["footImg"];
            object count = cbll.InsertOrUpdate(type, recipe);
            return CommonMethod.FormatJsonObj(count, "");
        }

        /// <summary>
        /// 上传图片
        /// </summary>
        /// <returns></returns>
        public string loadFile()
        {
            try
            {
                var file = Request.Files[0];
                //类型
                string code = Request["code"] + "";
                string paths = "";
                switch (code)
                {
                    case "rispe":
                        paths = "/manager/rispeImg/";
                        break;
                    case "news":
                        paths = "/manager/newsImg/";
                        break;
                    case "tongZhi":
                        paths = "/manager/tongZhiImg/";
                        break;
                }
                if (!Directory.Exists(Server.MapPath("~" + paths)))
                {
                    Directory.CreateDirectory(Server.MapPath("~" + paths));
                }
                string newName = Guid.NewGuid() + Path.GetExtension(file.FileName);
                Bitmap bt = new Bitmap(Image.FromStream(file.InputStream), 400, 400);
                bt.Save(Server.MapPath("~" + paths) + newName);
                return CommonLib.CommonMethod.FormatJsonObj("上传成功", paths + newName);
            }
            catch (Exception ex)
            {
                return CommonLib.CommonMethod.FormatJsonObj("上传失败", ex.Message);
            }

        }
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <returns></returns>
        [YouErYuanManage.Filter.IsLogin]
        public string DelData()
        {
            RecipeBLL cbll = new RecipeBLL();
            TRecipe tr = new TRecipe();
            tr.id= Request["id"] + "";
            if (tr.id == "")
            {
                 return CommonLib.CommonMethod.FormatJsonObj("错误", "");
            }
            int count = cbll.DelData(tr);
            return CommonLib.CommonMethod.FormatJsonObj(count, "");
        }

        //得到类型
        public string getTypes()
        {
            RecipeBLL cbll = new RecipeBLL();
            DataSet ds = cbll.getTypes();
            var data = new {
                recipe = ds.Tables[0],
                recipe_type = ds.Tables[1]
            };
            return CommonMethod.FormatJsonObj("", data);
          
        }
    }
}